/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月20日         Root.Lu
 */

package com.coocaa.launcher.pattern.normal.item;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.coocaa.launcher.framework.manager.configdata.data.LauncherAppItemTableData.AppEntryData;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.framework.utils.NativeImageCache;
import com.coocaa.x.framework.utils.NativeImageCache.SmartBitmap;
import com.coocaa.x.uipackage.SkinManager;
import com.tianci.appstore.R;
import com.tianci.appstore.utils.SkyConstants;

/**
 * TODO (write something)
 *
 * @author lu
 * @version 2014年12月23日
 * @see 参考转向，也就是相关主题
 */
public class AppEntry extends AppBaseItemView {
    private Drawable iconDrawable = null;
    // private String packageName = null;
    public static String viewType = "hot_seat_entry";

    public AppEntry(Context context) {
        super(context, viewType);
        // TODO Auto-generated constructor stub
        update();
    }

    private void setTitle(String title) {
        titleString = title;
        titleView.setText(title);
    }

    private void setIcon(Drawable drawable) {
        iconDrawable = drawable;
        iconView.setImageDrawable(drawable);
    }

    public void update(AppEntryData data) {
        this.data = data;
        if (data == null) {
            Toast.makeText(mContext, "data == null", Toast.LENGTH_LONG).show();
            return;
        }
        setTitle(data.getTitle());

        Drawable tempDrawable = null;
        if (data.getLocalPath() != null && data.getLocalPath().trim().length() > 0) {
            SmartBitmap bitmap = NativeImageCache.getInstance().getBitmapByPath(data.getLocalPath(),
                    SkyConstants.Div(144), SkyConstants.Div(144));
            //tempDrawable = Utils.loadDrawableFromPath(data.getLocalPath());
            tempDrawable = new BitmapDrawable(bitmap.get());
        }
        if (tempDrawable == null) {
            // decode pkgname icon
//            try
//            {
//                tempDrawable = mContext.getPackageManager().getApplicationIcon(
//                        data.getPackageName());
//            } catch (NameNotFoundException e)
//            {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }

            if (data.getPackageName().equals("com.tianci.appstore")) {
                try {
                    String mainActivity = data.getActivityName();
//                    ComponentName easyLauncher = new ComponentName(mContext.getPackageName(),
//                            mainActivity);
//                    tempDrawable = mContext.getPackageManager().getActivityIcon(easyLauncher);
                    tempDrawable = new BitmapDrawable(NativeImageCache.getInstance().getBitmapByPkg(
                            mContext, data.getPackageName(), mainActivity, SkyConstants.Div(144), SkyConstants.Div(144)).get());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    tempDrawable = new BitmapDrawable(NativeImageCache.getInstance().getBitmapByPkg(
                            mContext, data.getPackageName(), data.getMainActivity(), SkyConstants.Div(144), SkyConstants.Div(144)).get());
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
        this.iconDrawable = tempDrawable;
        setIcon(iconDrawable);

        if (data.getIconUrl() != null && data.getIconUrl().trim().length() > 0) {
            // download在线icon
        }
    }

    @Override
    public void onThemeChange() {
        // TODO Auto-generated method stub
        LogUtils.d("SSS", "onThemeChange!!!");
        // removeAllViews();
        update();
    }

    private void update() {
        removeAllViews();
        View view = SkinManager.getInstance().getLayout(R.layout.appitemview_appentry);
        addView(view);
        iconView = (ImageView) view.findViewById(R.id.appentry_imageview_icon);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) iconView.getLayoutParams();
        params.height = SkyConstants.Div(144);
        params.width = SkyConstants.Div(144);
        params.topMargin = SkyConstants.Div(20);
        iconView.setLayoutParams(params);
        SkinManager.getInstance().setBackgroundResource(iconView,
                R.drawable.icon_default_desktop);
        titleView = (TextView) view.findViewById(R.id.appentry_textview_title);

        titleView.setTextSize(SkyConstants.Dpi(29));
        titleView.setSelected(false);
        if (titleString != null)
            setTitle(titleString);
        if (iconDrawable != null)
            setIcon(iconDrawable);
    }

    // public final void setPackageName(String packageName)
    // {
    // this.packageName = packageName;
    // }
    //
    // public final String getPackageName()
    // {
    // return packageName;
    // }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    }
}
